package gttp.nspa.client.adm.event;

import gttp.nspa.shared.om.Customer;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

public class CustomerActionEvent extends GwtEvent<CustomerActionEvent.Handler> {

    public static enum ActionType {
        CREATED, UPDATED, DELETED;
    }

    public static interface Handler extends EventHandler {

        void onAction(CustomerActionEvent event);
    }

    public static CustomerActionEvent createdEvent(Customer cust) {
        return new CustomerActionEvent(ActionType.CREATED, cust);
    }

    public static CustomerActionEvent updatedEvent(Customer cust) {
        return new CustomerActionEvent(ActionType.UPDATED, cust);
    }

    public static CustomerActionEvent deletedEvent(Customer cust) {
        return new CustomerActionEvent(ActionType.DELETED, cust);
    }

    public static Type<Handler> TYPE = new Type<Handler>();

    public final ActionType     actionType;
    public final Customer       customer;

    private CustomerActionEvent(ActionType actionType) {
        this.actionType = actionType;
        this.customer = null;
    }

    private CustomerActionEvent(ActionType actionType, Customer cust) {
        this.actionType = actionType;
        this.customer = cust;
    }

    public Type<Handler> getAssociatedType() {
        return TYPE;
    }

    protected void dispatch(Handler handler) {
        handler.onAction(this);
    }

}
